# Maintainer: Antoine Martin <antoine@devloop.org.uk>

_realname=python-gssapi
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.6.5
pkgrel=1
pkgdesc="A python interface to RFC 2743/2744 (plus common extensions) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://github.com/pythongssapi/${_realname}'
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-decorator"
         "${MINGW_PACKAGE_PREFIX}-python-six"
         "${MINGW_PACKAGE_PREFIX}-gss"
         "${MINGW_PACKAGE_PREFIX}-cython")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/pythongssapi/python-gssapi/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha512sums=('81e32bd22f4fee6e6ce2e28b60053abd21e5691f5b9083c07437e42547bdd8e55e36b083546a14baa831e2cbe005a6145d09e33815297ddbdab4e1b5a3966a21')
export MSYS=winsymlinks:lnk

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  sed -i -e 's+elif winkrb_path:+elif False:+g' setup.py
  sed -i -e 's+gssapi/gssapi.h+gss.h+g' gssapi/raw/python_gssapi.h
  sed -i -e 's+gssapi/gssapi.h+gss.h+g' gssapi/raw/python_gssapi_ext.h
  sed -i -e 's+gssapi/gssapi_krb5.h+gss.h+g' gssapi/raw/python_gssapi_krb5.h
  cat > gssapi/raw/mech_krb5.pyx <<EOF
from libc.stdint cimport uint32_t
cdef extern from "python_gssapi.h":
    ctypedef uint32_t OM_uint32
    ctypedef struct gss_OID_desc:
        OM_uint32 length
        void *elements
    ctypedef gss_OID_desc* gss_OID
def make_OID(s):
    import struct
    v = struct.pack("@Is", len(s), s)
    return v
pkrb5 = make_OID(b"\x2A\x86\x48\x86\xF7\x12\x01\x02\x02")
cdef char *gss_mech_krb5_str = pkrb5
pprincipal = make_OID(b"\x2A\x86\x48\x86\xF7\x12\x01\x02\x02");
cdef char *GSS_KRB5_NT_PRINCIPAL_NAME_str = pprincipal
cdef gss_OID gss_mech_krb5 = <gss_OID> gss_mech_krb5_str
cdef gss_OID GSS_KRB5_NT_PRINCIPAL_NAME = <gss_OID> GSS_KRB5_NT_PRINCIPAL_NAME_str
EOF
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  GSSAPI_MAIN_LIB=${MINGW_PREFIX}/bin/libgss-3.dll \
  GSSAPI_LINKER_ARGS="-lgss" \
  GSSAPI_COMPILER_ARGS="-fPIC" \
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --skip-build

  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
